//
//  ViewController.swift
//  在屏幕上拖动图片
//
//  Created by quanhao huang on 2019/9/29.
//  Copyright © 2019 demo. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var draggedView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.draggedView.isUserInteractionEnabled = true
        
        let dragGest = UIPanGestureRecognizer(target: self, action:#selector(dragged(dragGesture:)))
        
        self.draggedView.addGestureRecognizer(dragGest)
    }

    @objc func dragged(dragGesture: UIPanGestureRecognizer) {
        if dragGesture.state == .began || dragGesture.state == .changed {
            var newPosition = dragGesture.translation(in: dragGesture.view!)
            
            newPosition.x += dragGesture.view!.center.x
            newPosition.y += dragGesture.view!.center.y
            
            dragGesture.view!.center = newPosition
            dragGesture.setTranslation(CGPoint.zero, in: dragGesture.view)
        }
    }

}

